package com.henry.customviewstudy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.dingmouren.layoutmanagergroup.skidright.SkidRightLayoutManager;
import com.henry.customviewstudy.R;

/**
 * Created by henry on 2018/6/7.
 */

public class SkidRightActivity extends AppCompatActivity {

    private RecyclerView myRecyclerView;
    private SkidRightLayoutManager skidRightLayoutManager;
    private MyAdapter myAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_skid);
        initView();
    }

    private void initView() {
        myRecyclerView = findViewById(R.id.recycler_view);
        skidRightLayoutManager = new SkidRightLayoutManager(1.5f, 0.85f);
        myAdapter = new MyAdapter();
        myRecyclerView.setLayoutManager(skidRightLayoutManager);
        myRecyclerView.setAdapter(myAdapter);
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private int[] imgs = {
                R.mipmap.skid_right_1,
                R.mipmap.skid_right_2,
                R.mipmap.skid_right_3,
                R.mipmap.skid_right_4,
                R.mipmap.skid_right_5,
                R.mipmap.skid_right_6,
                R.mipmap.skid_right_7,

        };
        String[] titles = {"Acknowl", "Belief", "Confidence", "Dreaming", "Happiness", "Confidence"};

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_skid_right, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyAdapter.ViewHolder holder, final int position) {
            Glide.with(getApplicationContext()).load(imgs[position % 7]).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.imgBg);
            holder.tvTitle.setText(titles[position % 6]);
            holder.imgBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(SkidRightActivity.this, SkidRightActivity_2.class);
                    intent.putExtra("img",imgs[position % 7]);
                    intent.putExtra("title",titles[position % 6]);
                    Pair<View, String> img_view = Pair.create((View)holder.imgBg, "img_view_1");
                    Pair<View, String> tv_title = Pair.create((View)holder.tvTitle, "title_1");
                    Pair<View, String> tv_bottom = Pair.create((View)holder.tvBottom, "tv_bottom");
                    ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(SkidRightActivity.this, img_view,tv_title,tv_bottom);
                    startActivity(intent,optionsCompat.toBundle());
                }
            });

        }

        @Override
        public int getItemCount() {
            return 20;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imgBg;
            TextView tvTitle;
            TextView tvBottom;

            public ViewHolder(View itemView) {
                super(itemView);
                imgBg = itemView.findViewById(R.id.img_bg);
                tvTitle = itemView.findViewById(R.id.tv_title);
                tvBottom = itemView.findViewById(R.id.tv_bottom);
            }
        }
    }
}
